Conditions | 24 |
Paths | 844 |
Total Lines | 132 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like stringFuncs.decodeCSV often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** global: UB */ |
||
128 | decodeCSV: function (headers, trimValues, columnar = false, seperator = "auto") { |
||
129 | var csvString = this.toString(); |
||
130 | |||
131 | // cut String into lines |
||
132 | var lines = csvString.trim().splitLines(); |
||
133 | var sep = seperator == "auto" ? UB.CSV_detectSeperator(csvString, lines.length) : seperator; |
||
134 | |||
135 | // config |
||
136 | var hasHeaders = headers != null; |
||
1 ignored issue
–
show
|
|||
137 | var returnAsObjs = headers.exists(); |
||
138 | |||
139 | // status |
||
140 | var inQuoted = false; |
||
141 | |||
142 | // result |
||
143 | var linesData = []; |
||
144 | var word = []; |
||
145 | var tempHeaders = []; |
||
146 | |||
147 | // per line |
||
148 | for (var l = 0, ll = lines.length; l < ll; l++) { |
||
149 | var line = lines[l]; |
||
150 | var isHeader = (l == 0 && hasHeaders); |
||
1 ignored issue
–
show
|
|||
151 | |||
152 | // if we are in quoted text |
||
153 | if (inQuoted) { |
||
154 | |||
155 | // keep taking chars |
||
156 | |||
157 | }else{ |
||
158 | |||
159 | // save words into headers array / new array |
||
160 | var lineWords = []; |
||
161 | if (isHeader){ |
||
162 | if (returnAsObjs){ |
||
163 | lineWords = tempHeaders; |
||
164 | }else{ |
||
165 | lineWords = headers; |
||
166 | } |
||
167 | } |
||
168 | if (!isHeader) { |
||
169 | linesData.push(lineWords); |
||
170 | } |
||
171 | |||
172 | } |
||
173 | |||
174 | // per char |
||
175 | for (var c = 0, clast = line.length - 1; c <= clast; c++) { |
||
176 | var ch = line.charAt(c); |
||
177 | |||
178 | // if we are in quoted text |
||
179 | if (inQuoted) { |
||
180 | |||
181 | // quotes.. |
||
182 | if (ch == "\"") { |
||
183 | |||
184 | // quote may be escaped |
||
185 | if (line.charAt(c + 1) == "\"") { |
||
186 | c++; |
||
1 ignored issue
–
show
|
|||
187 | word.push("\""); |
||
188 | }else { |
||
189 | |||
190 | // quote means ending quoted text |
||
191 | inQuoted = false; |
||
192 | } |
||
193 | |||
194 | continue; |
||
195 | } |
||
196 | |||
197 | // normal char |
||
198 | word.push(ch); |
||
199 | |||
200 | |||
201 | }else { |
||
202 | |||
203 | // quote means beginning quoted text |
||
204 | if (ch == "\""){ |
||
205 | inQuoted = true; |
||
206 | continue; |
||
207 | } |
||
208 | |||
209 | // comma means end of word |
||
210 | if (ch == sep) { |
||
211 | lineWords.push(trimValues ? word.join("").trim() : word.join("")); |
||
212 | word = []; |
||
213 | continue; |
||
214 | } |
||
215 | |||
216 | // normal char |
||
217 | word.push(ch); |
||
218 | |||
219 | // newline means end of word |
||
220 | if (c == clast) { |
||
221 | lineWords.push(trimValues ? word.join("").trim() : word.join("")); |
||
222 | word = []; |
||
223 | } |
||
224 | |||
225 | } |
||
226 | |||
227 | } |
||
228 | |||
229 | // at end of line take word |
||
230 | if (!inQuoted && word.Length > 0) { |
||
231 | lineWords.push(trimValues ? word.join("").trim() : word.join("")); |
||
232 | word = []; |
||
233 | } |
||
234 | } |
||
235 | |||
236 | // convert array to objs |
||
237 | if (returnAsObjs){ |
||
238 | |||
239 | // go thru all rows |
||
240 | for (var l = 0, ll = linesData.length; l < ll; l++) { |
||
241 | var line = linesData[l]; |
||
242 | var obj = {}; |
||
243 | |||
244 | // convert all cells to obj props |
||
245 | for (var h = 0, hl = headers.length; h < hl; h++) { |
||
246 | var header = headers[h]; |
||
247 | obj[header] = line[h]; |
||
248 | } |
||
249 | linesData[l] = obj; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | // convert 2D array to columnar |
||
254 | if (columnar && !returnAsObjs) { |
||
255 | linesData = linesData.transpose(); |
||
256 | } |
||
257 | |||
258 | return linesData; |
||
259 | }, |
||
260 | |||
331 |